package com.atguigu.gmall.product;

import java.util.concurrent.*;

/**
 * 注意单个方法测试 因为许多方法存在异步 造成输出结果混乱不知道哪个方法的
 */
public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //一般不用默认线程池创建方式
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        ExecutorService threadPool = new ThreadPoolExecutor(2,
                5,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        //1.开启新线程 无返回值
        CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "----runAsync()----run----");
        }, threadPool);
        //2.开启新线程 有返回值
        CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "----supplyAsync()----run----");
            return "supplyAsync";
        }, threadPool);
        System.out.println(supplyAsync.get());
        //3.开启新线程 一般不用
//        CompletableFuture<String> java = CompletableFuture.completedFuture("java");
//        System.out.println("----supplyAsync()----run----" + java.get());
    }
}
